package com.lhgray.sglhome.presentation.ui.compose

import android.content.Context
import android.content.Intent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.lhgray.sglhome.domain.model.Source
import com.lhgray.sglhome.domain.model.SourceData
import com.lhgray.sglhome.domain.model.UserData
import com.lhgray.sglhome.presentation.ui.activity.UploadPhotoActivity
import com.lhgray.sglhome.presentation.viewModel.HomeViewModel
import com.lhgray.sglhome.presentation.viewModel.PicUpStates
import com.lhgray.sglhome.presentation.viewModel.PicUploadState
import com.lhgray.sglhome.presentation.viewModel.PicUploadViewModel

/**
 * 上传图片的页面
 */
@Preview(showBackground = true)
@Composable
fun PreviewView() {
    var username by remember { mutableStateOf("") }
}

private fun jumpUpload(context: Context, source: Source?) {
    source?.let {
        val intent = Intent(context, UploadPhotoActivity::class.java)
        intent.putExtra("source_table", source.source_table)
        intent.putExtra("source_id", source.source_id.toString())
        context.startActivity(intent)
    }

}

@Composable
fun PicUploadPage(
    homeViewModel: HomeViewModel,
    onScanBarcode: () -> Unit,//跳转到扫一扫的页面的
    viewModel: PicUploadViewModel = hiltViewModel(),
) {
    val context = LocalContext.current
    val fileNo by homeViewModel.fileNo.collectAsState()
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()
    val userData by homeViewModel.userData.collectAsStateWithLifecycle()

    homeViewModel.uploadLoadingOrError(uiState.isLoading,uiState.errorMsg ?: "")
    PicUploadCompose(
        viewModel,
        {
            homeViewModel.upFileNo(it)
            viewModel.onSearchNumber(it)
        },
        onScanBarcode,
        uiState.sourceData ?: SourceData(),
        { jumpUpload(context, uiState.sourceData?.source) },
        fileNo,
        userData
    )
    LaunchedEffect(fileNo) {
        if (fileNo.isNotEmpty()) {
            viewModel.onSearchNumber(fileNo)
        }
    }

}

@Composable
fun PicUploadCompose(
    viewModel: PicUploadViewModel,
    onSearchNumber: (String) -> Unit,
    onScanBarcode: () -> Unit,//跳转到扫码
    orderList: SourceData,//列表展示的数据
    onUploadClick: () -> Unit, //图片上传的操作
    fileNo: String,
    userData: UserData,
) {

    Column(Modifier.background(color = Color.White).fillMaxSize()) {
        UserHandleView(userData)
        InputSearch(fileNo, onScanBarcode, onSearchNumber)
        //列表的数据
        Column(
            modifier = Modifier
                .weight(1.0f)
                .fillMaxSize()
        ) {
            LazyColumnOrder(orderList.list_item_label)
        }

        // 图片上传按钮
        Button(
            onClick = onUploadClick,
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            shape = RoundedCornerShape(8.dp),
            colors = ButtonDefaults.buttonColors(Color(0xFF1A73E8))
        ) {
            Text(
                "Picture Upload", color = Color.White,
                style = TextStyle(
                    textAlign = TextAlign.Center,
                    fontSize = 16.sp,
                ),
                modifier = Modifier
                    .align(Alignment.CenterVertically)
            )
        }

    }
}